package com.ah.hf.controller;

import com.ah.hf.common.R;
import com.ah.hf.dto.DishDto;
import com.ah.hf.entity.Category;
import com.ah.hf.entity.Dish;
import com.ah.hf.entity.DishFlavor;
import com.ah.hf.entity.Employee;
import com.ah.hf.service.CategoryService;
import com.ah.hf.service.DishFlavorService;
import com.ah.hf.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.util.Base64Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.rmi.log.LogInputStream;

import javax.lang.model.element.NestingKind;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    //添加操作
    @PostMapping
    public R<String> dish(@RequestBody DishDto dishDto) {
        dishService.saveDish(dishDto);
        return R.success("添加成功");
    }

    //分页查询菜品和分类名称
    @GetMapping("/page")
    public R<Page> DishPage(int page ,int pageSize ,String name){
        Page<Dish> pageInfo = new Page(page,pageSize);

        Page<DishDto> dishDtoPage = new Page<>();

        //条件构造器
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper();

        //like查询,如果name不等于空，就执行查询
        qw.like(StringUtils.isNotEmpty(name),Dish::getName ,name);

        //排序条件
        qw.orderByAsc(Dish::getSort).orderByDesc(Dish::getCreateTime);

        //根据分页查询
        dishService.page(pageInfo, qw);

        //对象拷贝,除了records该属性不拷贝
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");

        List<Dish> records = pageInfo.getRecords();
        //使用stream流 lambda表达式
        List<DishDto> dishDtoList = records.stream().map((item)->{
            DishDto dishDto = new DishDto();

            //拷贝对象
            BeanUtils.copyProperties(item,dishDto);

            //分类id
            Long categoryId = item.getCategoryId();

            //根据id查询对象
            Category category= categoryService.getById(categoryId);
            if (category != null){
                String categoryName = category.getName();
                //取出name赋值给dishDto
                dishDto.setCategoryName(categoryName);
            }

            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(dishDtoList);

        return R.success(dishDtoPage);
    }

    //根据id查询菜品信息和口味信息
    @GetMapping("/{id}")
    public R<DishDto> update(@PathVariable Long id){
        DishDto dishDto = dishService.getByIDWithFlavor(id);
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> dishPut(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("成功");
    }

    //删除菜品操作
    @DeleteMapping
    public R<String> delete(Long ids){
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getId,ids);
        dishService.remove(qw);
        return R.success("删除成功");
    }

    //修改菜品转态
    @PostMapping("/status/{statusID}")
    public R<String> status(Long ids, @PathVariable int statusID){

        UpdateWrapper<Dish> uw = new UpdateWrapper<>();
        uw.eq("id",ids);
        uw.set("status",statusID);
        dishService.update(null,uw);
         return R.success("成功");
    }

    //根据条件查询菜品
    @GetMapping("/list")
    public R<List<Dish>> list(Dish dish){
        //根据categoryId查菜品
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        //判断是否为空
        qw.eq(dish.getCategoryId() != null,
                //查询条件
                Dish::getCategoryId, dish.getCategoryId());
        //菜品status(状态)为1(起售)
        qw.eq(Dish::getStatus,1);
        //按状态进行排序，在按时间进行排序
        qw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(qw);

        List<DishDto> dishDtoList = null;
        return R.success(list);
    }

   /* @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        DishDto dishDto = new DishDto();
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(dish != null,Dish::getCategoryId,dish.getCategoryId());
        Dish dish1 = dishService.getOne(qw);
        BeanUtils.copyProperties(dish1,dishDto);

        LambdaQueryWrapper<DishFlavor> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(qw1);
        dishDto.setFlavors(list);

        return null;
    }*/
}
